<?php
session_start();
$userID = $_SESSION['userID'];
$login = $_SESSION['login'];

mysql_connect('localhost', 'root');
mysql_select_db('dots');

$query = "SELECT battleID, autorID, fieldID, turn, guestID FROM battles WHERE (autorID=$userID OR guestID=$userID) AND finish=0";
$result = mysql_query($query);

if (!$result)
{
	header($main_page);
	exit;
}

$row = mysql_fetch_array($result);
if (!$row)
{
	header($main_page);
	exit;
}

$battleID = $row[0];
$player_color = ($row[1] == $userID)?1:2;
$current_player = $row[3]+1;
switch ($row[2])
{
	case 1:
		$maxX = 15;
		$maxY = 10;
		break;
	case 2:
		$maxX = 30;
		$maxY = 20;
		break;
	case 3:
		$maxX = 60;
		$maxY = 40;
		break;
	case 4:
		$maxX = 120;
		$maxY = 80;
		break;
}

if ($row[1] == $userID)
	$query = 'SELECT login FROM users WHERE userID='.$row[4];
else
	$query = 'SELECT login FROM users WHERE userID='.$row[1];

$result = mysql_query($query);

if (!$result)
{
	header($main_page);
	exit;
}

$row1 = mysql_fetch_array($result);
if (!$row1)
{
	header($main_page);
	exit;
}

$guest = $row1[0];

print "battleID=$battleID&";
print "player_color=$player_color&";
print "curr_player=$current_player&";
print "maxX=$maxX&";
print "maxY=$maxY&";
if ($row[1] == $userID)
{
	print "login=$login&";
	print "guest=$guest&";
}
else
{
	print "guest=$login&";
	print "login=$guest&";
}

$file = fopen('../battles/battle'.$battleID.'.txt', 'r');
$line = fread($file, 100000);
print 'field='.$line;


fclose($file);
?>